'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en',
        'datetimepicker'], function ($) {
  
  return {
  
    init: function () {
      this.initSearch();
      this.initCreate();
      this.initEdit();
      this.initDelete();
    },
      
    initSearch: function() {
      $(document).on("shown.bs.modal", "#search", function (e) {
      	$(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
      });
    },
    
    initCreate: function() {
      
      var that = this;
      
      $(document).on('shown.bs.modal', '#create', function(e) {
        
        that.exist(null);
        
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  // bind validate
        
        var $form = $(e.currentTarget).find("form")              //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
        
      });
      
    },
    
    initEdit: function() {
      
      var that = this;
      
      $(document).on('shown.bs.modal', '#edit', function(e) {
        
        var existCode = $('input[name="code"]').val().trim();
        that.exist(existCode);
        
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  // bind validate
        
        var $form = $(e.currentTarget).find("form")              //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
        
      });
      
    },
    
    initDelete: function() {
      
      $(document).on("shown.bs.modal", "#delete", function (e) {
        
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
  
        var $form = $(e.currentTarget).find("form")              //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
  
      });
      
    },
    
    exist: function(existCode) {
      
      $(document).on('blur', 'input[name="code"]', function(e) {
        
        var $btnSubmit = $('button.btn-submit');
        var code = $('input[name="code"]').val().trim();
        
        var sameCode = existCode == code;
        if (sameCode) return;
        
        $.ajax({
          'url': window.__CTX__.root + "/teams/exist",
          'data': {
            'code': code
          }
        }).done(function(res){
          
          res = typeof res === 'String' ? JSON.parse(res) : res;
          if (res.data.exist) {
            $("input[name='code']").show_err_tooltip('该车队代码已存在！');
            $btnSubmit.prop('disabled', true);
            return;
          }
          
          $("input[name='code']").show_ok_tooltip('该车队代码不存在！');
          $btnSubmit.prop('disabled', false);
          
        }).fail(function(){
          $("input[name='code']").show_err_tooltip('查询车队代码失败！');
          $btnSubmit.prop('disabled', true);
        });
        
      });
      
    }
  
  }
  
})